package cn.tedu.ivos.geofence.controller;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.pojo.CirclemapDTO;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParm;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@Api(tags = "围栏模块")
@RequestMapping("/v1/geofence")
public class GeofenceController {
    @Autowired
    private GeofenceService geofenceService;
    @Autowired
    private VehicleMapper vehicleMapper;

    @ApiOperation("查询围栏")
    @GetMapping("select")
    public JsonResult selectgeofence(GeofenceQuery geofenceQuery){
        List<GeofenceVO> list = geofenceService.selectgeofence(geofenceQuery);
        //之前在围栏管理页只查询了围栏名称/坐标/创建时间/状态,并没有对车辆信息进行处理
        //现在需要给围栏补充车辆总数与可用车辆的数据
        for(int i=0;i<list.size();i++){
            //依次取出每个遍历的围栏对象
            GeofenceVO geofenceVO = list.get(i);
            //查询当前围栏上绑定的所有车辆
            VehicleQuery vehicleQuery = new VehicleQuery();
            //设置车辆的查询条件为:当前遍历到的围栏id
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //获取当前围栏的绑定车辆总数
            int totalNum = vehicleVOList.size();
            //再定义变量保存可用车辆数
            int availableNum = 0;
            for (VehicleVO vo : vehicleVOList){
                //车辆状态 1空闲 2占用 3维修
                if (vo.getStatus().equals("1")){
                    availableNum++;
                }
            }
            //给当前围栏对象设置可用车辆数,如果可用车辆数>0,则说明有可用车辆,否则车用车辆数为0
            if (availableNum>0){
                geofenceVO.setAvailableNum(availableNum);
            }else {
                geofenceVO.setAvailableNum(0);
            }
            //给围栏对象设置车辆总数
            geofenceVO.setTotalNum(totalNum);
            //后续进一步查看车辆详情时会使用车辆数据,所以一起存入geofenceVO返回给前端
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return JsonResult.ok(list);
    }

    @ApiOperation("添加围栏")
    @PostMapping("save")
    public JsonResult  saveGeofence(GeofenceParm geofenceParm){
        log.debug("添加围栏：geofenceParm={}",geofenceParm);
        geofenceService.saveGeofence(geofenceParm);
        return JsonResult.ok();
    }

    @ApiOperation("删除围栏")
    @PostMapping("delete/{id}")
    public JsonResult deleteGeofence(@PathVariable Long id){
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        if (list!=null && list.size()>0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        geofenceService.deleteGeofence(id);
        return JsonResult.ok();
    }
}
